<template>
  <div>
<h1>注册页面</h1>
<table>
  <tr>
    <td>账号</td>
    <td><input type="text" v-model="state.userName"></td>
  </tr>
  <tr>
    <td>密码</td>
    <td><input type="password" v-model="state.userPwd"></td>
  </tr>
  <tr>
    <td></td>
    <td><input type="button" value="登录" @click="zhu"></td>
  </tr>
</table>
  </div>
</template>

<script setup lang="ts">
import {reactive,toRefs,onMounted} from 'vue'
import axios from 'axios'
import {useRoute,useRouter} from 'vue-router'
const router=useRouter()
const route=useRoute()

let data=reactive({
  state:{
    userName:'',
    userPwd:''
  }
})

let {state}=toRefs(data)
//注册功能
const zhu=()=>{
//验证
if(state.value.userName==""){
  alert('账号不能为空');
  return;
}
if(state.value.userPwd==""){
  alert('密码不能为空');
  return;
}

  axios({
    url:'/api/Users/PostAddUser',
    method:'post',
    data:state.value
  })
  //判断用户是否注册成功
  .then(res=>{
    if(res.data.data==-1){
      alert('该用户已存在');
      return;
    }
    else if(res.data.data>0){
      alert('注册成功');
      router.push("/login");
    }
    else{
      alert('注册失败');
    }
  })
  //异常处理
.catch(err=>{
console.log(err);

})
}
</script>

<style scoped>

</style>